import 'package:boss/models/store_menu.dart';
import 'package:boss/provider/commodity_provider.dart';
import 'package:boss/ui/widget/alert/alert.dart';
import 'package:boss/ui/widget/back_confirmation.dart';
import 'package:boss/ui/widget/center_title_app_bar.dart';
import 'package:boss/ui/widget/label_button.dart';
import 'package:flutter/material.dart';
import 'package:hi_common/hi_common.dart';
import 'package:provider/provider.dart';
import 'package:router/annotation.dart';

import 'provider/classification_detail_provider.dart';
import 'widget/classification_detail_browser.dart';

@RouterConfig(ClassificationDetailPage.pageName)
class ClassificationDetailPage extends StatefulWidget {
  static const pageName = '/classificationDetailPage';

  @Autowired()
  final StoreMenu storeMenu;

  const ClassificationDetailPage({this.storeMenu, Key key}) : super(key: key);

  @override
  _ClassificationDetailPageState createState() => _ClassificationDetailPageState('分类管理详情');
}

class _ClassificationDetailPageState extends BaseState<ClassificationDetailPage> {
  _ClassificationDetailPageState(String pageName) : super(pageName);

  bool get isEdit => widget?.storeMenu?.menuId != null;

  _deleteStoreMenu() async {
    var commodityProvider = Provider.of<CommodityProvider>(context, listen: false);

    var result = await commodityProvider.deleteStoreMenu(widget.storeMenu);

    if (result) {
      Navigator.pop(context);
    }
  }

  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(
      create: (ctx) => ClassificationDetailProvider(menu: widget.storeMenu),
      child: BackConfirmation(
        backConfirmationText: '分类还未完成确认返回么？',
        child: Scaffold(
          appBar: CenterTitleAppBar(
            title: isEdit ? '分类详情' : '新增分类',
            backConfirmationText: '分类还未完成确认返回么？',
            actions: <Widget>[
              if (isEdit)
                LabelButton(
                  title: '删除',
                  padding: EdgeInsets.only(left: 8, top: 2, bottom: 2),
                  onTap: _deleteStoreMenu,
                ),
            ],
          ),
          body: ClassificationDetailBrowser(),
        ),
      ),
    );
  }
}
